//
//  ViewController.m
//  ShaoPingWuLiu
//
//  Created by mac on 15/11/5.
//  Copyright (c) 2015年 WPJoe. All rights reserved.
//

#import "ViewController.h"
#import "WPGetToken.h"
#import "ControllerManager.h"

@interface ViewController ()
@property (nonatomic, strong) UIScrollView * introScrollView;/**< 引导图滚动视图 */
@property (nonatomic, strong) NSArray * imagesArr;/**< 引导图数组 */
@property (nonatomic, strong) UIButton * joinBtn;/**< 立即体验 */
@property (nonatomic, strong) UIImageView * launchSecreenImageView;/**< 闪屏 */
@property (nonatomic, strong) UILabel * addTokenStatusLabel;/**< 加载用户token状态文本 */
@property (nonatomic, strong) UIButton * refreshBtn;/**< 刷新按钮 */
- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getToken];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeDataSource {
    self.imagesArr = @[@"yd1.png", @"yd2.png", @"yd3.png"];
}
- (void)initializeUserInterface {
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.launchSecreenImageView];
    [self.view addSubview:self.addTokenStatusLabel];
}
- (void)addIntroScrollView {
    [self initializeDataSource];
    [self.view addSubview:self.introScrollView];
    [self configImageView];
}
- (void)getToken {
    __weak typeof(self)weakself = self;
    [WPGetToken getTokenWithSuccessBlock:^(NSString *succ) {
        if ([[NSUserDefaults standardUserDefaults]boolForKey:@"firstLogin"]) {
            [weakself respondsToJoinBtn];
        } else {
            [weakself addIntroScrollView];
        }
    } andWithFailureBlock:^(NSString *fail) {
        weakself.addTokenStatusLabel.text = @"加载配置文件失败\n请刷新试试";
        if (!weakself.refreshBtn.superview) {
            [weakself.view addSubview:weakself.refreshBtn];
        }
        weakself.refreshBtn.hidden = NO;
    }];
}
#pragma mark - responds events
- (void)respondsToJoinBtn {
    [self presentViewController:[ControllerManager sharedManager].rootViewController animated:YES completion:^{
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"firstLogin"];
    }];
}
- (void)respondsToRefreshBtn:(UIButton *)sender {
    sender.hidden = YES;
    [NetWorkingManager cancelAllOperations];
    [self getToken];
    _addTokenStatusLabel.text = @"加载配置文件中...";
}
#pragma mark - private methods
- (void)configImageView {
    for (int i = 0; i < self.imagesArr.count; i++) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*320, 0, 320, ORIGIN_HEIGHT) adjustWidth:NO];
        imageView.image = IMAGE_NAME(self.imagesArr[i]);
        if (i == self.imagesArr.count - 1) {
            imageView.userInteractionEnabled = YES;
            [imageView addSubview:self.joinBtn];
        }
        [self.introScrollView addSubview:imageView];
    }
}
#pragma mark - getter
- (UIScrollView *)introScrollView {
    if (!_introScrollView) {
        _introScrollView = ({
            UIScrollView * view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, ORIGIN_HEIGHT) adjustWidth:NO];
            view.showsHorizontalScrollIndicator = NO;
            view.showsVerticalScrollIndicator = NO;
            view.contentSize = DHFlexibleSize(CGSizeMake(ORIGIN_WIDTH * self.imagesArr.count,  0), NO);
            view.pagingEnabled = YES;
            view.bounces = NO;
            view;
        });
    }
    return _introScrollView;
}
- (UIButton *)joinBtn {
    if (!_joinBtn) {
        _joinBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.bounds = CGRectMake(0, 0, 200, 50);
            btn.center = CGPointMake(self.view.center.x, SCREEN_SIZE.height - 80*DHFlexibleVerticalMutiplier());
            ;
            [btn setTitle:@"立即体验" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToJoinBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _joinBtn;
}
- (UIImageView *)launchSecreenImageView {
    if (!_launchSecreenImageView) {
        _launchSecreenImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
            view.image = IMAGE_NAME(@"shantu");
            view;
        });
    }
    return _launchSecreenImageView;
}
- (UILabel *)addTokenStatusLabel {
    if (!_addTokenStatusLabel) {
        _addTokenStatusLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_SIZE.height - 150, SCREEN_SIZE.width, 30)];
            lab.textAlignment = NSTextAlignmentCenter;
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.numberOfLines = 0;
            lab.text= @"加载配置文件中...";
            lab;
        });
    }
    return _addTokenStatusLabel;
}
- (UIButton *)refreshBtn {
    if (!_refreshBtn) {
        _refreshBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.bounds = CGRectMake(0, 0, 200, 50);
            btn.center = CGPointMake(self.view.center.x, SCREEN_SIZE.height - 100);
            ;
            [btn setTitle:@"刷新" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToRefreshBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _refreshBtn;
}
@end
